<?php

class RoleController extends Controller{

    private $M_roles;
    private $M_cates;
    private $M_user_has_roles;
    private $M_cate_has_roles;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_roles = Model::getInstance('roles');
        $this->M_cates = Model::getInstance('cates');
        $this->M_user_has_roles = Model::getInstance('user has roles');
        $this->M_cate_has_roles = Model::getInstance('cate has roles');
    }

    public function index(){
        global $controller;

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $role_name = $this->input()->post('role_name');
        $filter = array();

        if($role_name){
            $filter['role_name'] = $role_name;
        }

        $list =$this->M_roles->getRoleList($curr_page,$per_page,$filter);
        $data = array(
            'controller'=>$controller,
            'list'=>$list,
        );
        Template::show('role/index.php',$data);
    }

    public function add(){
        global $controller;

        $data = array(
            'controller'=>$controller
        );
        Template::show('role/add.php',$data);
    }

    public function del(){

        $role_id = $this->input()->get('role_id');
        $ret = $this->M_roles->delRole($role_id);

        if($ret){
            $this->M_cate_has_roles->delByRoleId($role_id);
            $this->M_user_has_roles->delByRoleId($role_id);
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"role",
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    public function edit(){
        global $controller;

        $role_id = (int)$this->input()->get('role_id');

        $role_info = $this->M_roles->get($role_id);
        $data = array(
            'controller'=>$controller,
            'role_info'=>$role_info,
        );

        Template::show('role/edit.php',$data);
    }

    public function set(){

        $role_id = (int)$this->input()->get('role_id');

        $roles = $this->M_cate_has_roles->getCateHasRoles($role_id);

        $cate_ids = array();
        foreach($roles as $v){
            $cate_ids[] = $v['cate_id'];
        }

        $menu_arr = $this->M_cates->getTree();

        $data = array(
            'role_id'=>$role_id,
            'menu_arr'=>$menu_arr,
            'cate_ids'=>$cate_ids,
        );

        Template::show('role/role_cate_tree.php',$data);
    }

    public function getUserHasRoles($user_id){
        return $this->M_user_has_roles->getUserHasRoles($user_id);
    }

    public function ajaxLookUp(){
        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $role_name = $this->input()->post('role_name');
        $filter = array();

        if($role_name){
            $filter['role_name'] = $role_name;
        }

        $list = $this->M_roles->getRoleList($curr_page,$per_page,$filter);

        $parent_rel  = $this->input()->get('rel');
        $data = array(
            'parent_rel'=>$parent_rel,
            'list'=>$list,
        );
        Template::show('role/role_lookup.php',$data);
    }

    public function ajaxSet(){
        $cate_ids = $this->input()->post('cate_ids');
        $role_id = $this->input()->post('role_id');

        $this->M_cate_has_roles->delByRoleId($role_id);

        if($cate_ids){
            $cate_ids = array_unique($cate_ids);
            foreach($cate_ids as $v){
                if($v){
                    $this->M_cate_has_roles->addCateHasRoles($v,$role_id);
                }
            }
        }

        $data = array(
            "statusCode"=>"200",
            "message"=>"权限设置成功!",
            "navTabId"=>"role",
            "callbackType"=>"closeCurrent"
        );
        echo Response::JSON(200,$data);
    }

    public function ajaxAdd(){

        $role_name = $this->input()->post('role_name');

        if(!$role_name){
            $data = array(
                "statusCode"=>"300",
                "message"=>"必须填写角色名",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_roles->addRole($role_name);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"role",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    public function ajaxEdit(){

        $role_id = $this->input()->post('role_id');
        $role_name = $this->input()->post('role_name');

        if(!$role_id || !$role_name){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_roles->updateRole($role_id,$role_name);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"修改成功",
                "navTabId"=>"role",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"修改失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }
}